
<div >
<!--  <div>-->
<!--    <button type="button" class="layui-btn" id="importJsonData">导入数据</button>-->
<!--    <button type="button" class="layui-btn layui-btn-danger" id="globalDisable">禁用表单</button>-->
<!--    <button type="button" class="layui-btn layui-btn-normal" id="globalNoDisable">启用表单</button>-->
<!--    <button type="button" class="layui-btn layui-btn-warm" id="getFormData" >获取表单数据</button>-->
<!--  </div>-->
  <div  id="testdemo" style="margin: 20px 20px;padding: 20px 20px;border: 1px solid #009688;">

  </div>
</div>
<div class="importjsoncodedataview" style="display: none;">
  <textarea class="site-demo-text" id="import-json-code-view"></textarea>
  <a href="javascript:" class="layui-btn layui-btn-normal" style="margin-right:20px;" id="import-json-code-data">导入数据</a>
</div>
<div class="getFormData" style="display: none;">
  <textarea class="site-demo-text" id="get-form-data"></textarea>
</div>


<script>
  var demojs = [];
  //JavaScript代码区域
  layui.use(['formDesigner', 'form','layer','upload','element'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var upload  = layui.upload;
    var element = layui.element
    var index = layui.index;
    var formDesigner = layui.formDesigner;
    var form = layui.form;
    var render;
    var $view = $('#testdemo')
    if (POPUP_DATA !== undefined) {
      demojs = POPUP_DATA
      render = formDesigner.render({
        elem: '#testdemo',
        data: demojs,
        viewOrDesign:true,
        previewElem:'#testdemo'
        //formData: {"textarea_1":"123","input_2":"123","password_3":"123"}
      });
      var curBtnId
      var images = render.getImages();
      // 上传图片初始化
      var uploadInst = upload.render({
        elem: '#commonUploadImgBtn',
        url: ctx +'formDesign/upload',
        accept: "images",
        method: 'post',
        multiple: true,
        size: 5120,
        before: function (obj) {
          layer.msg('图片上传中...', {
            icon: 16,
            shade: 0.01,
            time: 0
          })
        },
        done: function (res) {
          layer.close(layer.msg());//关闭上传提示窗口
          if(res.code == 200) {
            $view.find('#uploader-list-' + curBtnId).append(
                    '<div id="" class="file-iteme" style="position: relative;border: 1px solid #eee;">' +
                    '<i class="layui-icon layui-icon-close" onclick="removeImg(event)" data-id="'+ curBtnId +'" style="font-size: 21px;color:red;position: absolute;right: -2px;top: -6px;z-index: 999;cursor: pointer;"></i>' +
                    '<img style="width: 100px;height: 100px;" src=' + ctx + res.data.filePath + '>' +
                    '<div class="info">' + res.data.fileName + '</div>' +
                    '</div>'
            );
          }
        },
        error: function (index) {

        },allDone: function () {

        }
      })
      // 上传图片按钮点击
      $view.find('.uploadImgBtn').on('click',function (){
        curBtnId = $(this).attr('data-id')
        uploadInst.reload();
        $('#commonUploadImgBtn').click()
      })
      window.removeImg =  function (event){
        let id = $(event.target).data('id')
        $(event.target).parent().remove()
      }
      // 初始化上传文件
      var filesData = render.getFiles();
      for (var i = 0; i < filesData.length; i++) {
        let elem = $view.find('#' + filesData[i].select)
        let elemList = $view.find('#list-' + filesData[i].id)
        let actionBtn = $view.find('#listAction-' + filesData[i].id)
        upload.render({
          elem: elem
          , elemList: elemList //列表元素对象
          , url: '' + filesData[i].uploadUrl + ''
          , accept: 'file'
          , multiple: true
          , number: 3
          , auto: false
          , bindAction: actionBtn
          , choose: function (obj) {
            var that = this;
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
              var tr = $(['<tr id="upload-' + index + '">'
                , '<td>' + file.name + '</td>'
                , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                , '<td>'
                , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                , '</td>'
                , '</tr>'].join(''));

              //单个重传
              tr.find('.demo-reload').on('click', function () {
                obj.upload(index, file);
              });

              //删除
              tr.find('.demo-delete').on('click', function () {
                delete files[index]; //删除对应的文件
                tr.remove();
                // uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
              });
              // $($view.find('#list-' + filesData[i].id)).append(tr)

              that.elemList.append(tr);
              element.render('progress','progress-demo-' + index); //渲染新加的进度条组件
            });
          }
          , done: function (res, index, upload) { //成功的回调
            var that = this;
            //if(res.code == 0){ //上传成功
            var tr = that.elemList.find('tr#upload-' + index)
                    , tds = tr.children();
            tds.eq(3).html(''); //清空操作
            delete this.files[index]; //删除文件队列已经上传成功的文件
            return;
            //}
            this.error(index, upload);
          }
          , allDone: function (obj) { //多文件上传完毕后的状态回调
            console.log(obj)
          }
          , error: function (index, upload) { //错误回调
            var that = this;
            var tr = that.elemList.find('tr#upload-' + index)
                    , tds = tr.children();
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
          }
          , progress: function (n, elem, e, index) {
            element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
          }
        });
      }
    }
    //监听提交
    form.on('submit(demo1)', function(data){
      var json = render.getFormData();

      let dataT = render.getOptions()
     let img =  render.getImages()
      let file = render.getFiles()
      let edit =  render.geticeEditorObjects()
      let datas =  render.getData()

      debugger
      layer.msg( JSON.stringify(json), {icon: 6});
      /*$.ajax({
        url:"/activiti-manager/addActivitiManagera",
        type:"POST",
        data:temp,
        contentType:"application/json",
        dataType: "json",
        success:function(res){
          alert(res.message);
        }
      });*/
      return false;
    });

    //导入数据
    $('#importJsonData').on('click', function () {
      let obj = {
        file_1:'files/formDesign/20240830/64ca2384b1384d0eb7353370445bfb3d.txt',
        file_5:'files/formDesign/20240830/64ca2384b1384d0eb7353370445bfb3d.txt',
        image_4:'files/formDesign/20240830/d503df618e32499bb0008ca49288e07d.png',
        input_1 :"测试",
        rate_1: "3",
        rate_2: "3",
        rate_3: "3",
        rate_4: "3",
        select_3:"value2"
      }
      debugger
      render.setFormData(obj)
      // layer.open({
      //   type: 1
      //   , title: 'JSON模板数据导入'
      //   , id: 'Lay_layer_importjsoncodeview'
      //   , content: $('.importjsoncodedataview')
      //   , area: ['800px', '640px']
      //   , shade: false
      //   , resize: false
      //   , success: function (layero, index) {
      //   }
      //   , end: function () {
      //     $('.importjsoncodeview').css("display","none")
      //   }
      // });
    });

    //导入数据
    $('#getFormData').on('click', function () {
      var _value = render.getFormData();
      $('#get-form-data').val(JSON.stringify(_value,null,4));
      layer.open({
        type: 1
        , title: 'JSON模板数据导入'
        , id: 'Lay_layer_importjsoncodeview'
        , content: $('.getFormData')
        , area: ['800px', '640px']
        , shade: false
        , resize: false
        , success: function (layero, index) {
        }
        , end: function () {
          $('.getFormData').css("display","none")
        }
      });
    });

    $('#import-json-code-data').on('click', function () {
      var _value = document.getElementById("import-json-code-view").value;
      try {
        var json = JSON.parse(_value);
        render.setFormData(json);
        layer.closeAll();
        layer.msg('导入成功');
      }catch (e) {
        layer.closeAll();
        layer.msg('导入数据格式异常');
      }
    });

    $('#globalDisable').on('click', function () {
      render.globalDisable();
    });
    $('#globalNoDisable').on('click', function () {
      render.globalNoDisable();
    });
  });

</script>